package demo.virtuemart.category;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.support.events.internal.EventFiringMouse;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import demo.virtuemart.Util.MouseListener;
import demo.virtuemart.Util.TestUtil;

public class AddProductTest  extends TestSuiteBase{
	String [] runmode=null;
	static int count = -1;
	static boolean pass=true;
	static boolean fail=false;
	static boolean skip=false;
	@BeforeTest
	public void checkTestSkip() throws Throwable 
	{
		if(!TestUtil.isTestCaseRunnable(suite_category_xls, this.getClass().getSimpleName()))
		{
			logs.debug("Log Message---- Skipping the test case" +this.getClass().getSimpleName()+"as the run mode set to no");
			skip=true;
			throw new SkipException("Skipping the test case" +this.getClass().getSimpleName()+"as the run mode set to no");
		}
		runmode = TestUtil.getDataSetRunModes(suite_category_xls, this.getClass().getSimpleName());
	}
	
	@Test(dataProvider ="getTestData" )
	public void addProductToBasket(String mobiletype,
								String color,
								String memory,
								String engraving,
								String quantity, 
								String runmodes)
	{
		count++;
		if(!runmode[count].equalsIgnoreCase("Y"))
		{	
			skip=true;
		throw new SkipException("Run mode for test set data set " +count + "is set to no");
		}
		logs.debug("Executing Adding Product Test");
		logs.debug(mobiletype+"------"+color+ "----"+memory +"-------" +engraving+"-----"+quantity+"-----"+runmodes);
	sessionData.put("mobile_"+count, mobiletype);
	openBrowser();
	MouseListener listen = new MouseListener();
	EventFiringMouse mouse = new EventFiringMouse(driver,listen);
	driver.navigate().to(props.getProperty("testSiteName"));
	driver.manage().window().maximize();
	if(!checkElementPresence("virtueMart2Menu"))
	{
		fail=true;
		return;
	}

	getObject("virtueMart2Menu").click();
	driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);

	new FluentWait<WebDriver>(driver)
	.withTimeout(30, TimeUnit.SECONDS)
	.pollingEvery(5, TimeUnit.SECONDS)
	.ignoring(NoSuchElementException.class).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(OR.getProperty("categoryLayout"))));
	Locatable locate = (Locatable)driver.findElement(By.xpath(OR.getProperty("categoryLayout")));


	Coordinates cordi = locate.getCoordinates();
	mouse.mouseMove(cordi);
	mouse.click(cordi);

	driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

	if(!verifyTitle("Category Layout"))
	{
		fail=true;
		return;
	}
	getObject("mobilelink").click();
	driver.findElement(By.linkText(mobiletype)).click();
	getObject("qty").clear();
	int qtity = (int)Double.parseDouble(quantity);
	getObject("qty").sendKeys(String.valueOf(qtity));
	getObject("addtoCart").click();
	/*Set<String> win = driver.getWindowHandles();
	Iterator<String> iterate = win.iterator();
	List<String> winlist = new ArrayList();
	
	for(int i=0;i<win.size();i++)
	{
		winlist.add(iterate.next());
	}*/
	//driver.switchTo().window(winlist.get(1));
	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	
	if(!mobiletype.equals("Desire HD"))
	{
		String text= driver.findElement(By.xpath("//*[@id='facebox']/div/div/div/div")).getText();
		System.out.println(text);
	driver.findElement(By.xpath("//*[@id='facebox']/div/div/div/a[1]")).click();
	//getObject("continue_shopping").click();
	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}
	//closeBrowser();
	else
	{
		String text= driver.findElement(By.xpath("//*[@id='facebox']/div/div/div/div")).getText();
		System.out.println(text);
		return;
	}
		
	}
	
	@DataProvider
	public Object[][] getTestData() throws Throwable
	{
		return TestUtil.getData(suite_category_xls, this.getClass().getSimpleName());
	}
	@AfterMethod
	public void report() throws Throwable
	{
		if(skip)
		{
			TestUtil.reportDataSetResult(suite_category_xls, this.getClass().getSimpleName(), count+2, "Skip");
		}
		else if(fail)
		{
			  TestUtil.reportDataSetResult(suite_category_xls, this.getClass().getSimpleName(), count+2, "Fail");
		}
		else
		{
			for(int i=2;i<TestUtil.getRowNum(suite_category_xls, this.getClass().getSimpleName());i++)
			  TestUtil.reportDataSetResult(suite_category_xls, this.getClass().getSimpleName(), count+i, "Pass");
		}
		skip=false;
		fail=false;
		//pass=false;
	}
	@AfterTest
	public void reportTestResult() throws Throwable
	{
		if(pass)
			TestUtil.reportDataSetResult(suite_category_xls, "Test Cases", TestUtil.getRowNum(suite_category_xls,this.getClass().getSimpleName())+1, "Pass");
			
		else
			TestUtil.reportDataSetResult(suite_category_xls, "Test Cases", TestUtil.getRowNum(suite_category_xls, this.getClass().getSimpleName())+1, "Fail");
	}
	
	
}
